<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加分类按钮 -->
    <el-card class="box-card">
      <el-row
        ><el-button type="primary" @click="getProductListTwo"
          >添加分类</el-button
        ></el-row
      >
      <el-row>
        <tree-table
          :data="receiveProductList.result"
          :columns="columns"
          :show-row-hover="false"
          :expand-type="false"
          index-text="#"
          :selection-type="false"
          show-index
          border
        >
          <template slot="isOk" slot-scope="scope">
            <i
              class="el-icon-success"
              style="color:blue;"
              v-if="!scope.row.cat_deleted"
            ></i>
            <i
              class="el-icon-error"
              style="color:red;"
              v-if="scope.row.cat_deleted"
            ></i>
          </template>
          <template slot="jurisdictionSort" slot-scope="scope">
            <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
            <el-tag type="success" v-if="scope.row.cat_level === 1"
              >二级</el-tag
            >
            <el-tag type="warning" v-if="scope.row.cat_level === 2"
              >三级</el-tag
            >
          </template>
          <template slot="operationCommodity" slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deleteProduct(scope.row)"
              >删除</el-button
            >
          </template>
        </tree-table>
      </el-row>
      <el-row>
        <!-- 分页控件 -->
        <el-pagination
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :current-page="productList.pagenum"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="productList.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="receiveProductList.total"
        >
        </el-pagination>
        <!-- //添加分类弹出框 --> </el-row
      ><el-dialog
        title="添加分类"
        :visible.sync="dialogVisible"
        width="30%"
        @close="closeProduct"
      >
        <el-form ref="form" label-width="100px">
          <el-form-item label="分类名称：">
            <el-input
              v-model="selectorCommodity.cat_name"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
          <el-form-item label="父级分类：">
            <el-cascader
              v-model="selectorCommodityClassification"
              :options="popupReceiveProductList"
              :props="selectorObject"
              @change="addSelectorChange"
              clearable
              filterable
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="postSelectorChange"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      productList: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },

      receiveProductList: {},
      columns: [
        { label: "分类名称", prop: "cat_name" },
        {
          label: "是否有效",
          type: "template",
          template: "isOk",
        },
        {
          label: "排序",
          type: "template",
          template: "jurisdictionSort",
        },
        {
          label: "操作",
          type: "template",
          template: "operationCommodity",
        },
      ],
      //定义弹出分类
      popupReceiveProductList: [],
      // 定义子分类数据
      selectorObject: {
        label: "cat_name",
        children: "children",
        value: "cat_id",
        checkStrictly: true,
      },
      // 获取选中数据ID
      selectorCommodityClassification: [],
      // 提交选中分类
      selectorCommodity: {
        cat_pid: 0,
        cat_name: "",
        cat_level: "",
      },
      dialogVisible: false,
    };
  },
  methods: {
    //获取全部分类列表
    async getProductList() {
      const { data: res } = await this.$http({
        url: "/categories",
        method: "get",
        params: {
          ...this.productList,
        },
      });
      this.receiveProductList = res.data;
    },
    // 获取添加分类的父级分类列表
    async getProductListTwo() {
      const { data: res } = await this.$http({
        url: "/categories",
        method: "get",
        params: {
          type: 2,
        },
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.popupReceiveProductList = res.data;
      this.dialogVisible = true;
    },
    // 添加分类
    addSelectorChange() {
      if (this.selectorCommodityClassification.length > 0) {
        this.selectorCommodity.cat_pid = this.selectorCommodityClassification[
          this.selectorCommodityClassification.length - 1
        ];
        this.selectorCommodity.cat_level = this.selectorCommodityClassification.length;
      } else {
        this.selectorCommodity.cat_pid = 0;
        this.selectorCommodity.cat_level = 0;
      }
    },
    async postSelectorChange() {
      if (this.selectorCommodityClassification == [])
        return this.$message("未做出修改");
      const { data: res } = await this.$http({
        method: "post",
        url: "categories",
        data: {
          cat_pid: this.selectorCommodity.cat_pid,
          cat_level: this.selectorCommodity.cat_level,
          cat_name: this.selectorCommodity.cat_name,
        },
      });
      if (res.meta.status !== 201) return this.$message.error(res.meta.msg);
      this.dialogVisible = false;
      this.getProductList();
    },
    // 关闭重置分类选择框
    closeProduct() {
      this.selectorCommodityClassification = [];
    },

    // 分页控件切换页面
    handleCurrentChange(val) {
      this.productList.pagenum = val;
      this.getProductList();
    },
    // 分页控件切换显示条数
    handleSizeChange(val) {
      this.productList.pagesize = val;
      this.getProductList();
    },
    //编辑分类
    //删除分类
    async deleteProduct(val) {
      const promptMessage = await this.$confirm(
        "此操作将永久删除该分类, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((_) => {
        this.$message({
          type: "info",
          message: "已取消删除",
        });
      });
      if (promptMessage == "confirm") {
        const { data: res } = await this.$http({
          url: `/categories/${val.cat_id}`,
          method: "delete",
        });
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg);

        this.$message.success(res.meta.msg);
        this.getProductList();
      }
    },
  },
  created() {
    this.getProductList();
  },
};
</script>

<style lang="less" scoped>
.el-card {
  .el-row {
    .el-button {
      margin-bottom: 15px;
    }
  }
}
</style>
